import time

import requests
from lxml import etree
import os

root = 'hdbz_img'
if not os.path.exists(root):
    os.mkdir(root)
thumb_root = os.path.join(root,'thumb_root')
if not os.path.exists(thumb_root):
    os.mkdir(thumb_root)
preview_root = os.path.join(root,'preview_root')
if not os.path.exists(preview_root):
    os.mkdir(preview_root)

url = "https://www.hdbz.net/"
html_res = requests.get(url)
# print(html_res.content.decode())
html = html_res.content.decode()
tree = etree.HTML(html)
parent_a = tree.xpath('//div[@class="clearfix pic-auto pic-list"]/a')
for item in parent_a:
    # 懒惰路径
    lazy_path = item.attrib['href']
    print('正在访问',lazy_path)
    # 进入详情页
    try:
        detail_res = requests.get(url=f"{url}{lazy_path}")
        detail_html = detail_res.content.decode()
        detail_tree = etree.HTML(detail_html)
        detail_path = detail_tree.xpath('//div[@class="pic-preview"]/img/@src')[0]
        detail_name = detail_tree.xpath('//div[@class="pic-preview"]/img/@alt')[0]
        # 下载预览图
        preview_img = requests.get(url=detail_path)
        with open(f'{preview_root}/{detail_name}.jpg','wb')as f:
            f.write(preview_img.content)

        # 下载缩略图
        thumb_path = item.xpath('.//img/@data-original')[0]
        thumb_name = item.xpath('.//img/@alt')[0]
        thumb_img = requests.get(thumb_path)
        with open(f'{thumb_root}/{thumb_name}.jpg','wb')as f:
            f.write(thumb_img.content)
            time.sleep(1)
        break
    except Exception as e :
        print('组合图不能保存')